<?php
	/**
	 * util 公用函数库
	 */
	
	/**
	 * ajax返回json包装函数
	 * @param  $code int code值
	 * @param  $msg  string  描述信息
	 * @param  $data  all 返回数据
	 */
	function returnAjaxJson($code, $msg, $data){
		$json = array();
		$json["code"] = $code;
		$json["msg"] = $msg;
		$json["data"] = $data;

		// 操作成功则插入log日志
		// if($code == 200){
		// 	insertLog();
		// }

		echo json_encode($json, JSON_UNESCAPED_UNICODE | JSON_NUMERIC_CHECK);
		exit;
	}

	// 验证用户格式（字母开头和数字和“_”和“-”组合6-16位内）
	function valifyUserName($name){
		return preg_match("/^[a-zA-z0-9_-]{5,16}$/", $name);
	}

	// 验证邮箱
	function valifyEmail($email){
		return preg_match("/^[a-z\d]+(\.[a-z\d]+)*@([\da-z](-[\da-z])?)+(\.{1,2}[a-z]+)+$/", $email);
	}

	// 验证value值范围
	function valifyRange($val, $range){
		$len = count($range);
		for($i=0; $i<$len; $i++){
			$item = $range[$i];
			if($item === $val){
				return true;
			}
		}
		return false;
	}

	// 验证是否有该权限
	function isPermission($model, $action){
		global $session;
		$menuList = unserialize($session->get("menuList"));
		$permission = strtolower("/" . $model . "/" . $action . "/");

		if(!array_key_exists($permission, $menuList)){
			returnAjaxJson(200, "您无权限操作此模块！", null);
		}
	}

	//不同环境下获取真实的IP
	function getIP(){
		static $realip = NULL;
		if($realip !== NULL){
		    return $realip;
		}
	    //判断服务器是否允许$_SERVER
	    if(isset($_SERVER)){    
	        if(isset($_SERVER["HTTP_X_FORWARDED_FOR"])){
	            $realip = $_SERVER["HTTP_X_FORWARDED_FOR"];
	        }elseif(isset($_SERVER["HTTP_CLIENT_IP"])) {
	            $realip = $_SERVER["HTTP_CLIENT_IP"];
	        }else{
	            $realip = $_SERVER["REMOTE_ADDR"];
	        }
	    }else{
	        //不允许就使用getenv获取  
	        if(getenv("HTTP_X_FORWARDED_FOR")){
	              $realip = getenv( "HTTP_X_FORWARDED_FOR");
	        }elseif(getenv("HTTP_CLIENT_IP")) {
	              $realip = getenv("HTTP_CLIENT_IP");
	        }else{
	              $realip = getenv("REMOTE_ADDR");
	        }
	    }

	    return $realip;
	}     

	// 插入log日志
	function insertLog(){
		global $db,$session;
		$loginUserId = $session->get("userId");
		$model = $_GET["model"];
		$action = $_GET["action"];
		$jsonData = json_decode(file_get_contents("php://input"));
		$IP = getIP();
		$permission = strtolower("/" . $model . "/" . $action . "/");

		// 登录操作不保存data数据
		if($permission == "/login/loginin/"){
			// 登录操作
			$menuId = 20;
		} else if($permission == "/admin/pwd/"){
			// 修改密码
			$jsonData = null;
			$menuId = 17;
		} else if($permission == "/login/loginout/"){
			// 退出操作
			return;
		} else if($permission == "/admin/getmenu/"){
			// 获取菜单操作
			$menuId = 22;
		} else {
			$menuList = unserialize($session->get("menuList"));
			$menuId = $menuList[$permission]["id"];
		}



		//设置password属性为null
		$pwd = "password";
		if($jsonData && property_exists($jsonData, $pwd)){
			$jsonData->$pwd = null;
		}

		// 插入log日志数据
		$db->query("insert into admin_log(
						userid,
						menuid,
						data,
						IP)
					values(
						:userId,
						:menuId,
						:data,
						:IP)",
					array(
						"userId"=>$loginUserId,
						"menuId"=>$menuId,
						"data"=>json_encode($jsonData),
						"IP"=>$IP
						));
	}
?>